from django.shortcuts import render

# Create your views here.
from rest_framework import serializers, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.status import HTTP_504_GATEWAY_TIMEOUT
from rest_framework_jwt.authentication import jwt_decode_handler
from six_it.models import Question
from six_it.serializers.question import QuestionSerializers
from rest_framework.views import APIView
from rest_framework.generics import CreateAPIView,GenericAPIView
from rest_framework.mixins import RetrieveModelMixin,CreateModelMixin,DestroyModelMixin
class QuestionAPIview(RetrieveModelMixin,CreateModelMixin,GenericAPIView):
    queryset = Question.objects.all()
    serializer_class = QuestionSerializers
    # permission_classes = [IsAuthenticated]
    lookup_field = 'id'
    def post(self,request):
        user = request.user.id
        title = request.data['title']
        content = request.data['content']
        labels = request.data['labels']
        try:
            question = Question.objects.create(user_id=user,
                                               title=title,
                                               content=content,
                                               )
        except Exception as e:
            return Response({"message": "数据库错误", "success": False})
        for i in labels:
            question.labels.add(i)
            # question.save()

        return Response({"message": "问题发布成功", "success": True})
    def get(self,request,id):
        question = self.get_object()
        question.visits += 1
        question.save()
        replies = question.replies.all()
        question.comment_question = []
        question.answer_question = []
        for i in replies:
            if i.type == 0:
                question.comment_question.append(i)
            else:
                question.answer_question.append(i)
        serliaer = self.get_serializer(question)
        return Response(serliaer.data)

class QuestionYesView(APIView):

    def put(self,request,id):
        try:
            token = self.request.auth
            toke_user = jwt_decode_handler(token)
            user_id = toke_user["user_id"]
        except Exception as e:
            return Response('请先登录', status=404)
        try:
            question = Question.objects.get(id=id)
        except Exception as e:
            return Response(status=HTTP_504_GATEWAY_TIMEOUT)
        question.useful_count += 1
        question.save()
        return Response({'message':'谢谢支持',"success": True})

class QuestionNoView(APIView):

    def put(self,request,id):
        try:
            token = self.request.auth
            toke_user = jwt_decode_handler(token)
            user_id = toke_user["user_id"]
        except Exception as e:
            return Response('请先登录', status=404)
        try:
            question = Question.objects.get(id=id)
        except Exception as e:
            return Response(status=HTTP_504_GATEWAY_TIMEOUT)
        if question.useful_count != 0:
            question.useful_count -= 1
        question.unuseful_count += 1
        question.save()
        return Response({'message':'下次加油',"success": True})

